package com.siyougame.business.service.inspectItem;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.siyougame.business.controller.admin.inspectItemImg.vo.InspectItemImgRespVO;
import com.siyougame.business.convert.inspectItemImg.InspectItemImgConvert;
import com.siyougame.business.dal.dataobject.inspectItemImg.InspectItemImgDO;
import com.siyougame.business.service.inspectItemImg.InspectItemImgService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.siyougame.business.controller.admin.inspectItem.vo.*;
import com.siyougame.business.dal.dataobject.inspectItem.InspectItemDO;
import com.siyougame.common.pojo.PageResult;

import com.siyougame.business.convert.inspectItem.InspectItemConvert;
import com.siyougame.business.dal.mysql.inspectItem.InspectItemMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static com.siyougame.common.exception.util.ServiceExceptionUtil.exception;
import static com.siyougame.business.enums.ErrorCodeConstants.*;

/**
 * 检查记录明细 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class InspectItemServiceImpl extends ServiceImpl<InspectItemMapper,  InspectItemDO> implements InspectItemService {

    @Resource
    private InspectItemMapper inspectItemMapper;
    @Resource
    private InspectItemImgService inspectItemImgService;
    @Override
    public Long createInspectItem(InspectItemCreateReqVO createReqVO) {
        // 插入
        InspectItemDO inspectItem = InspectItemConvert.INSTANCE.convert(createReqVO);
        inspectItemMapper.insert(inspectItem);
        // 返回
        return inspectItem.getId();
    }

    @Override
    public void updateInspectItem(InspectItemUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateInspectItemExists(updateReqVO.getId());
        // 更新
        InspectItemDO updateObj = InspectItemConvert.INSTANCE.convert(updateReqVO);
        inspectItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectItem(Long id) {
        // 校验存在
        this.validateInspectItemExists(id);
        // 删除
        inspectItemMapper.deleteById(id);
    }

    private void validateInspectItemExists(Long id) {
        if (inspectItemMapper.selectById(id) == null) {
            throw exception(INSPECT_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public InspectItemDO getInspectItem(Long id) {
        return inspectItemMapper.selectById(id);
    }

    @Override
    public List<InspectItemDO> getInspectItemList(Collection<Long> ids) {
        return inspectItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectItemRespVO> getInspectItemPage(InspectItemPageReqVO pageReqVO) {
        PageResult<InspectItemRespVO> data = InspectItemConvert.INSTANCE.convertPage(inspectItemMapper.selectPage(pageReqVO));
        for (InspectItemRespVO inspectItemRespVO : data.getList()) {
            List<InspectItemImgDO> list = inspectItemImgService.list(Wrappers.<InspectItemImgDO>lambdaQuery()
                    .eq(InspectItemImgDO::getInspectItemId, inspectItemRespVO.getId()));
            List<InspectItemImgRespVO> inspectItemImgRespVOS = InspectItemImgConvert.INSTANCE.convertList(list);
            inspectItemRespVO.setImgRespVOList(inspectItemImgRespVOS);
        }
        return data;
    }

    @Override
    public List<InspectItemDO> getInspectItemList(InspectItemExportReqVO exportReqVO) {
        return inspectItemMapper.selectList(exportReqVO);
    }

}
